package wechat.response.business.event.message;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wechat.bean.response.MessageBaseBean;
import wechat.bean.response.event.message.SubscribeEventBean;
import wechat.bean.response.message.reply.ReplyImageTextBean;
import wechat.bean.response.message.reply.ReplyImageTextItemBean;
import wechat.bean.response.message.reply.ReplyTextBean;
import wechat.resources.AppFilter;
import wechat.resources.InstanceResource;
import wechat.util.Configure;

/**
 * 
 * 关注与取消关注共用
 *
 */
public class SubscribeEventBusiness {
	private Logger log = LoggerFactory.getLogger(SubscribeEventBusiness.class);
	
	private SubscribeEventBean subscribeEventBean;
	private InstanceResource instanceResource;
	
	public SubscribeEventBusiness(SubscribeEventBean subscribeEventBean){
		this.subscribeEventBean = subscribeEventBean;
	}
	
	public SubscribeEventBusiness(String appid,SubscribeEventBean subscribeEventBean){
		if (appid != null) {
			this.instanceResource = AppFilter.chooseApp(appid);
		}
		this.subscribeEventBean = subscribeEventBean;
	}
	
	public SubscribeEventBusiness(String str) throws Exception{
		this.subscribeEventBean = new SubscribeEventBean(str);
		this.subscribeEventBean.transform();
	}
	
	public SubscribeEventBusiness(String appid,String str) throws Exception{
		if (appid != null) {
			this.instanceResource = AppFilter.chooseApp(appid);
		}
		this.subscribeEventBean = new SubscribeEventBean(str);
		this.subscribeEventBean.transform();

	}
	
	public SubscribeEventBusiness(InstanceResource instanceResource,String str) throws Exception{
		this.instanceResource = instanceResource;
		this.subscribeEventBean = new SubscribeEventBean(str);
		this.subscribeEventBean.transform();

	}

	public String getSubscribeInfo() throws Exception {

		String openId = subscribeEventBean.getFromUserName();

		ReplyTextBean replyTextBean = new ReplyTextBean();
		if (instanceResource != null) {
			replyTextBean.setFromUserName("");
			replyTextBean.setContent("");
		}
		else {
			replyTextBean.setFromUserName(Configure.getWechatid());
			replyTextBean.setContent("");
		}
		
		replyTextBean.setToUserName(openId);
		replyTextBean.setCreateTime(System.currentTimeMillis());
		

		return replyTextBean.buildXml();
	}
	
	
	public String getReplyNews(List<ReplyImageTextItemBean> articles) throws Exception{
		String openId = subscribeEventBean.getFromUserName();
		
		ReplyImageTextBean replyImageTextBean = new ReplyImageTextBean();
		
		replyImageTextBean.setFromUserName(instanceResource.getCorpid());	
		replyImageTextBean.setToUserName(openId);
		replyImageTextBean.setCreateTime(System.currentTimeMillis());
		replyImageTextBean.setMsgType(MessageBaseBean.Reply_Message_News);
		replyImageTextBean.setArticles(articles);
		
		return replyImageTextBean.buildXml();
	}

	public InstanceResource getInstanceResource() {
		return instanceResource;
	}
}
